﻿using AMAP.Areas.Supplier.Models.Account;
using AMAP.Domain;
using AMAP.ServiceInterfaces;
using System.Web.Mvc;
using System.Web.Security;

namespace AMAP.Areas.Supplier.Controllers
{
    public class AccountController : Controller
    {
        private IAmapService AmapService;
        private ICoordinatesService CoordinatesService;
        private ISupplierService SupplierService;
        private IUserService UserService;

        public AccountController(IAmapService AmapService, ICoordinatesService CoordinatesService, ISupplierService SupplierService, IUserService UserService)
        {
            this.AmapService = AmapService;
            this.CoordinatesService = CoordinatesService;
            this.SupplierService = SupplierService;
            this.UserService = UserService;
        }

        //
        // GET: /Supplier/Account/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            SupplierAccountCreateViewModel Model = new SupplierAccountCreateViewModel();
            return View(Model);
        }

        [HttpPost]
        public ActionResult Create(SupplierAccountCreateViewModel Model)
        {
            Amap ClosestAmap = this.AmapService.GetApplicationAMAP();
            Domain.Supplier Supplier = SupplierService.Create();
            if (ModelState.IsValid)
            {
                AutoMapper.Mapper.Map(Model, Supplier);
                AutoMapper.Mapper.Map(Model, Supplier.User);
                AutoMapper.Mapper.Map(Model, Supplier.User.Coordinates);
                Supplier.User.Coordinates = GeoCoder.SetGPSCoordinates(Supplier.User.Coordinates);

                //Check distance of the wanted amap
                double DistanceWantedAmap = GeoCoder.GetDistance(Supplier.User.Coordinates, ClosestAmap);
                double DistanceMin = double.MaxValue;
                foreach (Amap AmapX in this.AmapService.GetAllAsList())
                {
                    double DistanceX = GeoCoder.GetDistance(Supplier.User.Coordinates, AmapX);
                    if (DistanceX < DistanceMin)
                    {
                        DistanceMin = DistanceX;
                        ClosestAmap = AmapX;
                    }
                }
                //Save only if the amap is the most closest
                if (DistanceWantedAmap <= DistanceMin)
                {
                    Supplier.AmapId = ClosestAmap.Id;

                    SupplierService.Add(Supplier);
                    //Connect the registrated user
                    FormsAuthentication.SetAuthCookie(Model.Email, false);

                    EmailService.Send("florent.henry@gmail.com", "Inscription fournisseur - Les marrons", "Bonjour Florent,<br>Votre inscription a notre AMAP s'est bien déroulée.<br><br>Vous serez notifié de la validation de votre compte par un administrateur au plus tôt.");

                    return RedirectToRoute("Supplier_default", new { Controller = "Home", action = "Index" });
                }
            }

            Model.ClosestAmap = ClosestAmap;
            Model.WantedAmap = this.AmapService.GetApplicationAMAP();
            Model.FailSupplier = Supplier;

            return View(Model);
        }

        [Authorize(Roles = "Supplier")]
        public ActionResult Edit()
        {
            SupplierAccountEditViewModel Model = new SupplierAccountEditViewModel();
            Domain.User UserInfo = this.UserService.GetByEmail(User.Identity.Name);
            AutoMapper.Mapper.Map(UserInfo, Model);
            AutoMapper.Mapper.Map(UserInfo.Coordinates, Model);
            AutoMapper.Mapper.Map(UserInfo.Supplier, Model);

            return View(Model);
        }

        [HttpPost]
        [Authorize(Roles = "Supplier")]
        public ActionResult Edit(SupplierAccountEditViewModel Model)
        {
            Domain.User UserInfo = this.UserService.GetByEmail(User.Identity.Name);


            //Due to EF4 issues, we cannot automap ViewModel content into the Coordinates POCO
            //(which isn't really a POCO in this case but a EF Proxy object)

            UserInfo.FirstName = Model.FirstName;
            UserInfo.LastName = Model.LastName;
            UserInfo.Supplier.CompanyName = Model.CompanyName;
            UserInfo.Coordinates.Address1 = Model.Address1;
            UserInfo.Coordinates.Address2 = Model.Address2;
            UserInfo.Coordinates.City = Model.City;
            UserInfo.Coordinates.PhoneNumber = Model.PhoneNumber;
            UserInfo.Coordinates.PostalCode = Model.PostalCode;
            UserInfo.Coordinates = GeoCoder.SetGPSCoordinates(UserInfo.Coordinates);
            this.CoordinatesService.Update(UserInfo.Coordinates);

            return RedirectToRoute("Supplier_default", new { controller = "Home", action = "Index" });
        }
    }
}
